package com.cash.channel.svc.mq;

import com.cash.channel.api.constant.Constant;
import com.cash.channel.svc.logic.CallBackLogic;
import com.cash.channel.svc.logic.ChannelLogic;
import com.cash.channel.svc.logic.RepayLogic;
import com.cash.channel.svc.model.ApplyWithdrawalFlow;
import com.cash.channel.svc.model.ChannelInfo;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import geex.trans.model.RepayResultAmqp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by chengbingzheng on 2019/8/5.
 */
@Slf4j
@Component
public class DeductListener {

    @Autowired
    private RepayLogic repayLogic;

    @Autowired
    private ChannelLogic channelLogic;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private CallBackLogic callBackLogic;


    @RabbitListener(containerFactory = "rabbitListenerContainerFactory", bindings = {
            @QueueBinding(
                    // 绑定queue
                    value = @Queue(value = Constant.QUEUE_KEY.还款结果通知平台, durable = "true", autoDelete = "false"),
                    // 绑定exchange
                    exchange = @Exchange(value = "geex", durable = "true"),
                    key = Constant.QUEUE_KEY.还款结果通知平台)})
    public void receiveCommonPay(Message message) {
        try {
            // 实体接收
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
            // 字符串接收
            log.info("收到扣款反馈通知：" + message);
            RepayResultAmqp repayResultAmqp = objectMapper.readValue(message.getBody(), RepayResultAmqp.class);
            log.info("收到扣款反馈通知 repayResultAmqp:" + repayResultAmqp.toString());
            try {
                if (repayResultAmqp != null) {
                    ApplyWithdrawalFlow applyWithdrawalFlow = repayLogic.getChannelByWdId(repayResultAmqp.getAppId());
                    if(applyWithdrawalFlow!=null&&StringUtils.isNotBlank(applyWithdrawalFlow.getcChannel())){
                        ChannelInfo channelInfo = channelLogic.getChannelInfoByCode(applyWithdrawalFlow.getcChannel());
                        if (0 == channelInfo.getnCustom()) {
                            callBackLogic.payStatusCallBack(repayResultAmqp,applyWithdrawalFlow.getcChannel());
                        } else {
                            // TODO: 2019/11/5 第三方
                        }
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw e;
            }

        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

}
